# Author: Ahmed AlSum aalsum@cs.odu.edu
# Date: July 21, 2010

import urllib2,urllib

import sys, os, re

from dateutil import parser as dateparser
from baseHandler import *



login_url = "https://secure.diigo.com/sign-in"
end_point = "http://www.diigo.com/cached?url="
#cached_url = "http://www.diigo.com/cached/showpage/upload?upload_id="
cached_url = "http://www.diigo.com/cached/showpage/crawler?link_id="

class DiigoHandler(BaseProxyHandler):

    def fetch_changes(self, req, requri, dt=None):

	opener =    urllib2.build_opener( urllib2.HTTPCookieProcessor() )
        try:
	  credentials = req.headers_in['Credentials']
	except:
 	  credentials = ""
        if(credentials != ""):
          username = credentials.split("//")[0]
	  password = credentials.split("//")[1]
	  data = urllib.urlencode([('username', username), ('password',password)])
	  try:
	     login = opener.open(login_url,data)
	  except:
	     login = ""
        final_url = end_point + requri
        
        user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.10) Gecko/20100504 Firefox/3.5.10 (.NET CLR 3.5.30729)'

		
        headers = { 'User-Agent' : user_agent }
	changes = None
        try:
		response = opener.open(final_url)
       		the_page = response.read()
	except:
		return changes
	regex = re.compile("<div class=\"itemTitle\">.\d\d\d\d-\d\d-\d\d</div>", re.DOTALL)
	regex = re.compile("<div class=\"itemTitle\">*<div>", re.DOTALL)
        item_regex = re.compile("\"my_webpage_\d*\"|\"diigo_webpage_\d*\"")
        ids = re.findall(item_regex,the_page)
	regex_date =re.compile("\d\d\d\d-\d\d-\d\d")
	dates = re.findall(regex_date,the_page)
	i = 0
	changes = []
	for id in ids:
         	dtstr = dates[i]#= regex_date.search(dates[i]).group(0)
                dtstr += " 00:00:00 GMT"
                dtobj = dateparser.parse(dtstr)
                loc =  cached_url  + id.split("_")[2].replace("\"","")
                changes.append((dtobj, loc, {'last' : dtobj, 'obs' : 1, 'type' : 'observed'}))
		i+=1
  
        return changes
     

def handler(req):
    hdlr = DiigoHandler('diigo')
    return basehandler(req, hdlr)
